package com.jeeplus.mob.service;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.mob.common.StringHelper;
import com.jeeplus.mob.entity.mobpojo.CardrecordInfo;
import com.jeeplus.mob.entity.mobpojo.WaycardrecordInfo;
import com.jeeplus.mob.mapper.CardrecordInfoMapper;
import com.jeeplus.mob.mapper.WaycardrecordInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
@SuppressWarnings("")
public class WaycardrecordService {

    @Autowired
    private CardrecordInfoMapper cardrecordInfoMapper;

    @Autowired
    private WaycardrecordInfoMapper waycardrecordInfoMapper;

    /**
     * 在途打卡
     * @param waycardrecordInfo
     * @return
     */
    public AjaxJson punchWay(WaycardrecordInfo waycardrecordInfo) {
        CardrecordInfo cardrecordInfo = new CardrecordInfo();
        if (StringHelper.IsEmptyOrNull(waycardrecordInfo.getWaybatchNumber())) {
            String uuid = IdGen.uuid();
            waycardrecordInfo.setWaybatchNumber(uuid);
            int rows = waycardrecordInfoMapper.insertCardrecord(waycardrecordInfo);
            waycardrecordInfo.getId();
            if (rows > 0) {
                // 向打卡记录表中插入在途打卡数据用于在途打卡和楼宇打卡记录合并
                cardrecordInfo.setWaycardrecordId(waycardrecordInfo.getId().toString());
                cardrecordInfo.setPunchNumber(waycardrecordInfo.getWaybatchNumber());
                cardrecordInfo.setUserId(waycardrecordInfo.getUserId());
                cardrecordInfo.setPunchDate(waycardrecordInfo.getPunchDate());
                cardrecordInfo.setPunchPicture(waycardrecordInfo.getPunchPicture());
                cardrecordInfo.setPunchAddress(waycardrecordInfo.getPunchAddress());
                cardrecordInfo.setPunchType("1");
                cardrecordInfo.setCreateBy(waycardrecordInfo.getUserId());
                cardrecordInfo.setCreateDate(waycardrecordInfo.getPunchDate());
                cardrecordInfo.setUpdateBy(waycardrecordInfo.getUserId());
                cardrecordInfo.setUpdateDate(waycardrecordInfo.getPunchDate());
                cardrecordInfo.setAuditStatus("0");
                cardrecordInfo.setDeleteFlag("0");
                int row = cardrecordInfoMapper.insertSelective(cardrecordInfo);
                if (row > 0) {
                    return AjaxJson.success("打卡成功！").put("data",waycardrecordInfo.getWaybatchNumber());
                }

            }
            return AjaxJson.success("打卡失败，请重试！");
        } else {

            int rows = waycardrecordInfoMapper.insertCardrecord(waycardrecordInfo);
            if (rows > 0) {
                cardrecordInfo.setWaycardrecordId(waycardrecordInfo.getId().toString());
                cardrecordInfo.setPunchNumber(waycardrecordInfo.getWaybatchNumber());
                cardrecordInfo.setUserId(waycardrecordInfo.getUserId());
                cardrecordInfo.setPunchDate(waycardrecordInfo.getPunchDate());
                cardrecordInfo.setPunchPicture(waycardrecordInfo.getPunchPicture());
                cardrecordInfo.setPunchAddress(waycardrecordInfo.getPunchAddress());
                cardrecordInfo.setPunchType("1");
                cardrecordInfo.setCreateBy(waycardrecordInfo.getUserId());
                cardrecordInfo.setCreateDate(waycardrecordInfo.getPunchDate());
                cardrecordInfo.setUpdateBy(waycardrecordInfo.getUserId());
                cardrecordInfo.setUpdateDate(waycardrecordInfo.getPunchDate());
                cardrecordInfo.setAuditStatus("0");
                cardrecordInfo.setDeleteFlag("0");
                int row = cardrecordInfoMapper.insertSelective(cardrecordInfo);
                if (row > 0) {
                    return AjaxJson.success("打卡成功！").put("data",waycardrecordInfo.getWaybatchNumber());
                }

            }
            return AjaxJson.success("打卡失败，请重试！");
        }
    }
}
